<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  

  <script type="text/javascript">
    
    var arr = [
      {name:'a', sex:'male', xueli:'bk'},
      {name:'b', sex:'female', xueli:'zk'},
      {name:'c', sex:'female', xueli:'bk'},
      {name:'d', sex:'male', xueli:'zk'},
      {name:'e', sex:'male', xueli:'zk'},
      {name:'f', sex:'male', xueli:'bk'},
      {name:'g', sex:'female', xueli:'zk'},
      {name:'h', sex:'male', xueli:'bk'},
      {name:'i', sex:'female', xueli:'zk'},
      {name:'j', sex:'female', xueli:'zk'},
      {name:'k', sex:'male', xueli:'bk'},
    ];

// 根据sex属性男女分组
// 答案 { 'male': ['a', 'd', 'e'.......], 'female': ['b','c'.......] }

var _by = 'sex'
var _type = ['female','male']
var _obj = {}

for(var i = 0; i < arr.length; i++){
  if(arr[i][_by] == _type[0]){
    // 如果对象点出属性后是空，就初始化成一个数组
    if(!_obj[_type[0]]){
      _obj[_type[0]] = []
    }
    _obj[_type[0]].push(arr[i].name)
  }else{
    if(!_obj[_type[1]]){
      _obj[_type[1]] = []
    }
    _obj[_type[1]].push(arr[i].name)
  }
}
    
console.log(_obj);









var by = 'xueli'
var type = ['bk', 'zk']
var obj = {}

for(var i = 0; i < arr.length; i++){
  if(arr[i][by] == type[0]){
    // 如果对象点出属性后是空，就初始化成一个数组
    if(!obj[type[0]]){
      obj[type[0]] = []
    }
    obj[type[0]].push(arr[i].name)
  }else{
    if(!obj[type[1]]){
      obj[type[1]] = []
    }
    obj[type[1]].push(arr[i].name)
  }
}
    
console.log(obj);

  </script>
</body>
</html>